package com.cnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cnt.dao.DonationRequestDao;
import com.cnt.dao.DonationsDao;
import com.cnt.dao.EvaluateDao;
import com.cnt.entity.DonationRequest;
import com.cnt.entity.Donations;
import com.cnt.entity.Evaluate;
import com.cnt.service.EvaluateService;
import com.cnt.vo.EvaluateVo;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired
    private DonationRequestDao donationRequestDao;
    @Autowired
    private EvaluateDao evaluateDao;
    @Autowired
    private DonationsDao donationsDao;

    @Override
    public ResultVo<List<EvaluateVo>> getAllByDonationId(int donationId) {
        List<EvaluateVo> evaluateVos = new ArrayList<>();
        List<DonationRequest> donationRequestList = donationRequestDao.getByDonationId(donationId);
        for (int i = 0; i < donationRequestList.size(); i++) {
            DonationRequest donationRequest = donationRequestList.get(i);
            evaluateVos.add(new EvaluateVo(donationRequest.getRequestId(),
                    donationRequest.getDonationId(), donationRequest.getTime()
                    , donationRequest.getReply(), null));
        }
        if (donationRequestList.isEmpty()) {
            return new ResultVo<>(1, "没有历史评价信息", null);
        }
        for (EvaluateVo evaluateVo : evaluateVos) {
            List<Evaluate> requestList = evaluateDao.getByRequestId(evaluateVo.getRequestId());
            evaluateVo.setList(requestList);
        }
        return new ResultVo<>(0, "查询成功", evaluateVos);
    }

    @Override
    public ResultVo<Void> addEvaluateByRequestId(Evaluate evaluate) {
        Integer requestId = evaluate.getRequestId();
        Integer userId = evaluate.getUserId();
        LambdaQueryWrapper<Evaluate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Evaluate::getUserId, userId)
                .eq(Evaluate::getRequestId, requestId);
        List<Evaluate> evaluates = evaluateDao.selectList(lqw);
        if (evaluates.size() >= 1) {
            return new ResultVo<>(3, "该管理员已经做出评价", null);
        }
        DonationRequest donationRequest = donationRequestDao.selectById(requestId);
        if (donationRequest.getReply() != null) {
            if ("".equals(donationRequest.getReply())) {
                int insert = evaluateDao.insert(evaluate);
                LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
                if (insert == 1) {
                    if (evaluate.getStatus().equals("通过")) {
                        luw.eq(DonationRequest::getRequestId, requestId)
                                .setSql("agree_count = agree_count + " + 1);
                        donationRequestDao.update(null, luw);
                    } else {
                        luw.eq(DonationRequest::getRequestId, requestId)
                                .setSql("reject_count = reject_count + " + 1);
                        donationRequestDao.update(null, luw);
                    }
                    LambdaUpdateWrapper<Donations> uw = new LambdaUpdateWrapper<>();
                    int donationId = donationRequest.getDonationId();
                    uw.eq(Donations::getDonationId, donationId)
                            .set(Donations::getStatus, 4);
                    donationsDao.update(null, uw);
                    return new ResultVo<>(0, "评价成功", null);
                }
                return new ResultVo<>(2, "评价失败", null);
            }

            return new ResultVo<>(1, "此募捐申请已做出了决定，评价失败", null);
        }
        int insert = evaluateDao.insert(evaluate);
        LambdaUpdateWrapper<DonationRequest> luw = new LambdaUpdateWrapper<>();
        if (insert == 1) {
            if (evaluate.getStatus().equals("通过")) {
                luw.eq(DonationRequest::getRequestId, requestId)
                        .setSql("agree_count = agree_count + " + 1);
                donationRequestDao.update(null, luw);
            } else {
                luw.eq(DonationRequest::getRequestId, requestId)
                        .setSql("reject_count = reject_count + " + 1);
                donationRequestDao.update(null, luw);
            }
            LambdaUpdateWrapper<Donations> uw = new LambdaUpdateWrapper<>();
            int donationId = donationRequest.getDonationId();
            uw.eq(Donations::getDonationId, donationId)
                    .set(Donations::getStatus, 4);
            donationsDao.update(null, uw);
            return new ResultVo<>(0, "评价成功", null);
        }
        return new ResultVo<>(2, "评价失败", null);
    }
}
